﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Design;
using System.Linq;
using System.Text;
using Osg;
using ZFGK.ComponentModels;
using ZFGK.OSG.Utility;
using ZFGK.Utility;
using OSS.BIM.Model;

namespace OSS.BIM.UI.PropertyGrids
{
    [TypeConverter(typeof(PropertySorter))]
    class ModelInfoBinding
    {
        /// <summary>
        /// 模型
        /// </summary>
        [Browsable(false)]
        public ModelElement ModelElement { get; set; }

        public ModelInfoBinding(ModelElement userModel)
        {
            ModelElement = userModel;
        }

        /// <summary>
        /// 名称
        /// </summary>
        [SortedCategory("基本", 1)]
        [DisplayName("名称")]
        [Description("名称")]
        [Browsable(true)]
        [ReadOnly(false)]
        [PropertyOrder(1)]
        public string Name
        {
            get { return ModelElement.Name; }
            set { ModelElement.Name = value; }
        }

        private Vec3d m_scale = null;
        /// <summary>
        /// 缩放
        /// </summary>
        [SortedCategory("基本", 1)]
        [DisplayName("缩放")]
        [Description("缩放")]
        [Browsable(false)]
        [ReadOnly(false)]
        public Vec3d Scale
        {
            get
            {
                if (m_scale == null)
                    m_scale = ModelElement.Scale.ParseVec3d();
                if (m_scale == null)
                    m_scale =  new Vec3d();
                return m_scale;
            }
            set
            {
                ModelElement.Scale = m_scale.ToString();
            }
        }

        /// <summary>
        /// 缩放
        /// </summary>
        [SortedCategory("基本", 1)]
        [DisplayName("缩放X")]
        [Description("缩放X")]
        [Browsable(true)]
        [ReadOnly(false)]
        [PropertyOrder(2)]
        public double ScaleX
        {
            get{ return Scale.x;}
            set
            {
                Scale.x = value;
                Scale = Scale;
            }
        }

        [SortedCategory("基本", 1)]
        [DisplayName("缩放Y")]
        [Description("缩放Y")]
        [Browsable(true)]
        [ReadOnly(false)]
        [PropertyOrder(3)]
        public double ScaleY
        {
            get { return Scale.y; }
            set
            {
                Scale.y = value;
                Scale = Scale;
            }
        }

        [SortedCategory("基本", 1)]
        [DisplayName("缩放Z")]
        [Description("缩放Z")]
        [Browsable(true)]
        [ReadOnly(false)]
        [PropertyOrder(4)]
        public double ScaleZ
        {
            get { return Scale.z; }
            set
            {
                Scale.z = value;
                Scale = Scale;
            }
        }

        private Vec3d m_location = null;
        [SortedCategory("基本", 1)]
        [DisplayName("位置")]
        [Description("位置")]
        [Browsable(false)]
        [ReadOnly(false)]
        public Vec3d Location
        {
            get
            {
                if (m_location == null)
                    m_location = ModelElement.Location.ParseVec3d();
                if (m_location == null)
                    m_location = new Vec3d();
                return m_location;
//                 var position = ModelElement.Location.ParseVec3d();
//                 return position == null ? new Vec3d() : position;
            }
            set { ModelElement.Location = m_location.ToString(); }
        }

        [SortedCategory("基本", 1)]
        [DisplayName("经度(°)")]
        [Description("经度(°)")]
        [Browsable(true)]
        [ReadOnly(false)]
        [PropertyOrder(5)]
        public double InsertPtX
        {
            get { return Location.x; }
            set
            {
                Location.x = value;
                Location = Location;
            }
        }

        [SortedCategory("基本", 1)]
        [DisplayName("纬度(°)")]
        [Description("纬度(°)")]
        [Browsable(true)]
        [ReadOnly(false)]
        [PropertyOrder(6)]
        public double InsertPtY
        {
            get { return Location.y; }
            set
            {
                Location.y = value;
                Location = Location;
            }
        }

        [SortedCategory("基本", 1)]
        [DisplayName("高程(m)")]
        [Description("高程(m)")]
        [Browsable(true)]
        [ReadOnly(false)]
        [PropertyOrder(7)]
        public double InsertPtZ
        {
            get { return Location.z; }
            set
            {
                Location.z = value;
                Location = Location;
            }
        }

        // 旋转角度
        [SortedCategory("基本", 1)]
        [DisplayName("旋转角度(°)")]
        [Description("旋转角度(°)")]
        [Browsable(true)]
        [ReadOnly(false)]
        [PropertyOrder(8)]
        public double Rotation
        {
            get
            {
                //double modelRotate = Math.Round(ConvertUtil.RadianToAngle(ModelElement.Rotation), 2);
                return ConvertUtil.RadianToAngle(ModelElement.Rotation);
            }
            set { ModelElement.Rotation = ConvertUtil.AngleToRadian(value); }
        }

        // 旋转角度
        [SortedCategory("基本", 1)]
        [DisplayName("高度偏移(m)")]
        [Description("高度偏移(m)")]
        [Browsable(true)]
        [ReadOnly(false)]
        [PropertyOrder(9)]
        public double ZOffset
        {
            get
            {
                return ModelElement.ZOffset;
            }
            set { ModelElement.ZOffset = value; }
        }
    }
}
